package com.mdd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.R;
import com.mdd.dto.DishDto;
import com.mdd.pojo.Category;
import com.mdd.pojo.Dish;
import com.mdd.pojo.DishFlavor;
import com.mdd.service.CategoryService;
import com.mdd.service.DishFlavorService;
import com.mdd.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> addDish(@RequestBody DishDto dishDto){
        dishService.saveDish(dishDto);
        return R.success("添加成功");

    }

    /**
     * 菜品的分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value = "dishCache",key = "#page+'_'+#pageSize")
    public R<Page<DishDto>> dishPage(int page,int pageSize,String name){
        //分页对象
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(name),Dish::getName,name).orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage,queryWrapper);
        //属性拷贝
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> records = dishPage.getRecords();
        List<DishDto> records1 = new ArrayList<>();
        dishDtoPage.setRecords(records1);
        for (Dish dish : records) {
            //创建DishDto对象
            DishDto dishDto = new DishDto();
            //将dish对象中的属性copy给dishDto
            BeanUtils.copyProperties(dish,dishDto);
            //根据dish的套餐id查询套餐
            Category category = categoryService.getById(dish.getCategoryId());
            //根据套餐对象得到套餐名设置给dishDto
            if (category!=null) {
                dishDto.setCategoryName(category.getName());
                records1.add(dishDto);
            }
        }
        return R.success(dishDtoPage);

    }

    /**
     * 根据id查询菜品和对应口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getDishAndFlavorById(@PathVariable("id") Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品及其口味
     * @param dishDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> updateDish(@RequestBody DishDto dishDto){
        dishService.updateDish(dishDto);
        return R.success("修改成功");
    }

    /**
     * 删除菜品和菜品对应的口味
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        dishService.deleteDishAndFlavor(ids);
        return R.success("删除成功!");
    }

    /**
     * 修改菜品的状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> updateStatus(@PathVariable("status") int status,@RequestParam("ids") List<Long> ids){
        if (dishService.updateDishStatus(status, ids)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 根据菜类id查询出相同类型的菜品
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> listByCategoryId(Dish dish){
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        //根据categoryId查询该分类的菜品
        queryWrapper.eq(dish!=null,Dish::getCategoryId,dish.getCategoryId()).eq(Dish::getStatus,1).orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        //获得该类的所有菜品
        List<Dish> list = dishService.list(queryWrapper);

        //获取该分类每一个菜品的口味信息，将信息封装到DishDto中
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish1 : list) {
            DishDto dishDto = new DishDto();
            //将菜品基本信息拷贝至dishDto
            BeanUtils.copyProperties(dish1,dishDto);

            //根据菜品id查询该菜品的口味信息
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,dish1.getId());
            List<DishFlavor> flavors = dishFlavorService.list(queryWrapper1);

            dishDto.setFlavors(flavors);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }

}
